import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1

import "getjson.js" as GetJson
import "getcoments.js" as GetCom
import "WebRequest.js" as Web
//import "goToPage.qml"
Page {
    width: parent.width
    height: parent.height
    lockInPortrait: true
    orientationLock: appWindow.lockposition
    tools: commonTools2
    //定义变量

    property bool hasmore:true
    property bool loading: false
    property string firsturl: "http://m2.qiushibaike.com/article/list/latest?count=30&page=1"
    property real pagecount
    ToolBarLayout {
        id: commonTools2
        visible: true
        ToolIcon{
             id:changecontent
             enabled: true
             iconSource: "images/title_func.png"
             visible:true
             width: 50
            anchors.left:parent.left
            anchors.verticalCenter:  parent.verticalCenter
            onClicked:
            (funmenu.status === DialogStatus.Closed) ? funmenu.open() : funmenu.close()
          }

        ToolIcon{
             id:imagecontent
             enabled: true
             iconSource: "images/icon_pic_enable.png"
             visible:true
             width: 50
            anchors.left:changecontent.right
            anchors.leftMargin: (righttoolicon.left-rechangecontent.left)/2+50
            anchors.verticalCenter:  parent.verticalCenter
           onClicked:{listModel.clear();GetJson.changecount(1)}
          }

        ToolIcon{

             id:rechangecontent
             enabled: true
             iconSource: "images/icon_new_enable.png"
             visible:true
             width: 50

            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter:  parent.verticalCenter
            onClicked:
            (contentmenu.status === DialogStatus.Closed) ? contentmenu.open() : contentmenu.close()
          }
        ToolIcon{

             id:gotowhere
             enabled: true
             iconSource: "images/icon_timeagain_enable.png"
             visible:true
             width: 50

            anchors.verticalCenter:  parent.verticalCenter
            anchors.left:rechangecontent.right
            anchors.leftMargin: (righttoolicon.left-rechangecontent.left)/2+50
            onClicked:tumberdialog.open()

          }



        ToolIcon {
            id:righttoolicon
            iconSource: "images/icon_post_enable.png"
           // visible:appWindow.qbtoken ===""?false:true
            width: 50
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked:
            {
                if(appWindow.qbtoken===""){
                    Web.showMessage("请登陆");
                    appWindow.pageStack.push(Qt.resolvedUrl("Login.qml"))
                }
                else{
                 appWindow.pageStack.push(Qt.resolvedUrl("ArticlePostPage.qml"))
                }

            }
        }


    }

    Menu{
        id:contentmenu
        opacity: 0.9
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
       // anchors.top: parent.top
        visualParent: pageStack
        MenuLayout {
        MenuItem {
            text: "查看最新"
            onClicked:{listModel.clear();GetJson.changecount(0)} }
      //  MenuItem { text: "查看图"
      //      onClicked:{listModel.clear();GetJson.changecount(1)} }
        MenuItem { text: "随便逛逛"
            onClicked:{listModel.clear();GetJson.changecount(2)} }
        MenuItem { text: "查看精华按日"
          onClicked:{listModel.clear();GetJson.changecount(3)}}
        MenuItem { text: "查看精华按周"
            onClicked:{listModel.clear();GetJson.changecount(4)}}
        MenuItem { text: "查看精华按月"
            onClicked:{listModel.clear();GetJson.changecount(5)}}
        MenuItem { text: "穿越"
            onClicked:{listModel.clear();GetJson.changecount(6)} }


        }

       }
     MultiSelectionDialog {
         id: dialog
         titleText:"退出?"
//         model: ListModel {
//             ListElement { name: "匿名发送" }
//             ListElement{name:"允许评论"}
//         }
         acceptButtonText: "退出"
         rejectButtonText: "取消"
         onAccepted: Qt.quit();
         }

     QueryDialog {
        id: aboutdialog
        icon:"images/icon_my_enable.png"
        titleText:"关于"
        message:  "糗事百科非官方客户端 0.2.6\n作者：log"
        acceptButtonText:"确定"


       // onAccepted: Qt.quit()
         }

    Menu {
        id: funmenu
        opacity: 0.9
        anchors.left: parent.left
        anchors.top: parent.top
        visualParent: pageStack


        MenuLayout {
            MenuItem { text: "个人中心"
                onClicked:{
                    appWindow.pageStack.push(Qt.resolvedUrl("UserCenterPage.qml"))    ;
                }
            }

            MenuItem { text: "设置"
               onClicked:{
                   GetCom.openFile("Setting.qml")

               }}

            MenuItem { text: "致谢"
                onClicked:appWindow.pageStack.push(Qt.resolvedUrl("Thanks.qml"))
            }
//            MenuItem { text: "选择图片"
//                onClicked:appWindow.pageStack.push(Qt.resolvedUrl("GalleryPage.qml"))
//            }

            MenuItem { text: "关于"
               // visible: false
                onClicked:aboutdialog.open() }
            MenuItem { text: "退出"
                onClicked:{
                    dialog.open();
                }
            }

        }
    }
    GoToPage{
        id:numbermodel
    }

    TumblerColumn {
        id: dayColumn
        //label: "MONTH"
        selectedIndex: 5
        items: numbermodel
    }
    TumblerColumn {
        id: yearColumn
        //label: "MONTH"
        selectedIndex: 0
        items: numbermodel
    }
    TumblerColumn {
        id: monthColumn
        //label: "MONTH"
        selectedIndex: 0
        items: numbermodel
    }
    TumblerDialog {
        id:tumberdialog
        titleText: "跳转页面"
        columns: [yearColumn, monthColumn,dayColumn]
        onAccepted: GetJson.callbackFunction()
    }







    Item{
        width:children.width
        height: children.height
        anchors.verticalCenter:  parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        visible: loading
        id: indicator1
    BusyIndicator {
                    id:indic
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: -40
                    running: true
             }
    Label{
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: indic.right
        anchors.leftMargin: 10
        platformStyle: LabelStyle{
        textColor:appWindow.labelcolor
        }

        text:"loading..."
    }

    }


    //定义页面



    //定义内容


        ListModel {  id:listModel }
        ListView
            {


                id:view
                property bool __allowRefresh: false
                property int sumcount:2000
                property bool __loadDone: !(sumcount>listModel.count)
                property bool loadMoreFlag: false

                anchors.fill:parent
                model : listModel
                delegate:maincontent
                onMovementStarted:testbtn.visible=false
                //onMovementEnded:testbtn.visible=true
                header: Component {
                       id: listHeader

                       Item {
                           width: view.width; height: 0

                           Text {
                               id: refreshTip
                               anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: refreshImage.verticalCenter }
                               color: "grey"
                               font.pixelSize: 25
                               text: view.__allowRefresh ? "释放立即刷新" : "下拉可以刷新"
                           }

                           Image {
                               id: refreshImage
                               anchors { bottom: parent.top; bottomMargin: 10; right: refreshTip.left; rightMargin: 50 }
                               source:  "images/pull-arrow.png"
                               rotation: {
                                   var coordY = view.visibleArea.yPosition * Math.max(view.height, view.contentHeight);
                                   if (coordY < -100 || view.__allowRefresh) {
                                       view.__allowRefresh = true;
                                       return -180;
                                   }
                                   else {
                                       return 0;
                                   }
                               }
                               Behavior on rotation { NumberAnimation { duration: 150 } }
                           }
                       }
                   }



                footer: Component {
                       Rectangle {
                           id: loadMoreTip

                           width: parent.width; height: 60
                           color: "transparent"
                           visible: view.model.count > 0

                           Text {
                               id: tipBody
                               anchors.centerIn: parent
                               color: "grey"
                               font.pixelSize: 25
                               text: view.__loadDone ? "全部加载完毕" : (view.loadMoreFlag ? "加载中" : "加载更多")
                           }

                           BusyIndicator {
                               anchors { verticalCenter: parent.verticalCenter; right: tipBody.left; rightMargin: 15 }
                               platformStyle : BusyIndicatorStyle {
                                   size: "medium"
                               }
                               running: true
                               visible: view.loadMoreFlag
                           }
                       }
                   }

                   onMovementEnded: {
                       testbtn.visible=true
                       if (view.atYBeginning) {
                           if (view.__allowRefresh) {
                               view.__allowRefresh = false;
                               mainPage.refresh();
                           }
                       }
                       else if (view.atYEnd) {
                           var count = listModel.count;
                           if (view.model.count > 0 && !view.__loadDone) {
                               mainPage.loadMore();
                           }
                       }
                   }

            }

            ContentCom{
                id:maincontent

            }

            Button {
                        id: testbtn
                        width: 60
                        height: 61
                        visible: listModel.count>10
                        anchors.right: parent.right
                        anchors.bottom: parent.bottom
                        anchors.bottomMargin: 100
                        //anchors.verticalCenter:  parent.verticalCenter
                        //anchors.horizontalCenter: parent.horizontalCenter
                        iconSource:"images/icon-m-toolbar-up.png"
                        //text: "回顶部"
                        opacity: 0.560
                        onClicked: {
                               //  console.log("currentIndex = :" +view.currentIndex)
                                   //      view.currentIndex =0;
                            view.positionViewAtIndex(0,ListView.Beginning)
                           // console.log("currentIndex = :" +view.currentIndex);
                                   // listview1.currentIndex = 0;
                                     }
            }



         Rectangle{
             id :imageview
             visible: false;
             anchors.verticalCenter:  parent.verticalCenter
             anchors.horizontalCenter: parent.horizontalCenter
             height: children.height>600?600:children.height;

             width: children.width>480?480:children.width;
             opacity: 1


             Flickable {
                 height: imageid.height>500?500:imageid.height;
                 width: imageid.width>480?480:imageid.width;
                 anchors.verticalCenter:  parent.verticalCenter
                 anchors.horizontalCenter: parent.horizontalCenter

                  contentWidth: imageid.width; contentHeight: imageid.height

                  Image {
                      anchors.verticalCenter:  parent.verticalCenter
                      anchors.horizontalCenter: parent.horizontalCenter
                      id :imagesmall;
                      source: "images/thumb_pic.png"
                      ProgressBar {
                          id: pb2
                          width:parent.width
                          //height:20
                          indeterminate: true
                          anchors.verticalCenter:  parent.verticalCenter
                          anchors.horizontalCenter: parent.horizontalCenter
                          //visible: simpletimer.running
                      }
                      MouseArea{
                          anchors.fill: parent;
                          onClicked: {
                               imageview.visible =false;
                          }
                      }



                  }

                  Image {
                 anchors.verticalCenter:  parent.verticalCenter
                 anchors.horizontalCenter: parent.horizontalCenter
                 id :imageid;
                 onStatusChanged: if (status===Image.Ready){

                                      imagesmall.source = ""
                                      imagesmall.visible = false
                                  }
                 MouseArea{
                     anchors.fill: parent;
                     onClicked: {
                          imageview.visible =false;
                     }
                 }

                 source: ""

             }
             }
         }
        Rectangle{
            id:qiushi
            visible: false
           // color: "lightblue"
            opacity: 0.7
            //anchors.fill: parent
            anchors.verticalCenter:  parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            width: 400
            height: children.height
        Text {
              id:qiushitext
             font.pointSize: 24
            color:appWindow.contntcolor
             anchors.top :parent.top
             wrapMode:Text.WordWrap
             width: parent.width
             }
        Button
               {
                   width:150
                   anchors.top: qiushitext.bottom
                   anchors.horizontalCenter: parent.horizontalCenter
                   text:"再试试？"
                   onClicked: {qiushi.visible = false;GetJson.load("");}

                }

    }
        function loadMore()
        {

            GetJson.getmore()
        }

        function refresh(){
            //console.log("i am runing!!!!!!!!");
            listModel.clear();
            GetJson.resfer("");
        }

        ScrollDecorator {
            flickableItem: view
        }
            Component.onCompleted:{
            //    appWindow.pageStack.push(mainPage);
                indicator1.visible=true; GetJson.load(firsturl)

            }
}
